package com.recommended.dto;

import com.recommended.constants.ResultConstants;

/**
 * 服务器返回客户端的结果对象，该对象为JSON格式
 */
public class ReturnResult {
    private static final int ERROR_CODE = 500;
    private static final int SUCCESS_CODE = 0;
    public static final int DUPLICATE_CODE = 300;

    private static final ReturnResult SUCCESS_RESULT = new ReturnResult(SUCCESS_CODE, ResultConstants.SUCCESS);
    private static final ReturnResult ERROR_RESULT = new ReturnResult(ERROR_CODE, ResultConstants.FAILURE);

    /**
     * 响应状态
     */
    private int code;

    /**
     * 响应状态原因描述
     */
    private String msg;

    /**
     * 返回数据总条数
     */
    private Long count;

    /**
     * 返回请求端的具体业务数据
     */
    private Object data;

    public ReturnResult(int code) {
        this(code, null);
    }

    public ReturnResult(int code, String msg) {
        this(code, msg, null);
    }

    public ReturnResult(int code, Object data) {
        this(code, null, data);
    }

    public ReturnResult(int code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public ReturnResult(int code, String msg, Long count, Object data) {
        this.code = code;
        this.msg = msg;
        this.count = count;
        this.data = data;
    }


    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Long getCount() {
        return count;
    }

    public void setCount(Long count) {
        this.count = count;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public static ReturnResult successResult() {
        return SUCCESS_RESULT;
    }

    public static ReturnResult successResult(Object data) {
        return successResult(null, data);
    }

    public static ReturnResult successResult(String msg, Object data) {
        return new ReturnResult(SUCCESS_CODE, msg, data);
    }

    public static ReturnResult successResult(long count, Object data) {
        return successResult(null, count, data);
    }

    public static ReturnResult successResult(String msg, Long count, Object data) {
        return new ReturnResult(SUCCESS_CODE, msg, count, data);
    }

    public static <T> ReturnResult successResultByPage(Page<T> page) {
        return new ReturnResult(SUCCESS_CODE, ResultConstants.SUCCESS, (long) page.getTotalCount(), page.getList());
    }

    public static ReturnResult errorResult() {
        return ERROR_RESULT;
    }

    public static ReturnResult errorResult(String msg) {
        return new ReturnResult(ERROR_CODE, msg);
    }

    public static ReturnResult errorResult(String msg, Object data) {
        return new ReturnResult(ERROR_CODE, msg, data);
    }

    public static ReturnResult result(boolean success) {
        if (success) {
            return SUCCESS_RESULT;
        }

        return ERROR_RESULT;
    }

    public static ReturnResult result(boolean success, String msg) {
        return result(success, msg, null);
    }

    public static ReturnResult result(boolean success, Object data) {
        return result(success, null, data);
    }

    public static ReturnResult result(boolean success, String msg, Object data) {
        if (success) {
            return successResult(msg, data);
        }

        return errorResult(msg, data);
    }

    public static ReturnResult result(int code, String msg) {
        return result(code, msg, null);
    }

    public static ReturnResult result(int code, String msg, Object data) {
        return new ReturnResult(code, msg, data);
    }
}
